//
//  ViewController.m
//  IMDemo
//
//  Created by KwokChun Cheung on 16/3/17.
//  Copyright © 2016年 eenet. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import <ImSDK/ImSDK.h>


#import "TIMHepler.h"

@interface ViewController ()<TIMMessageListener, TIMConnListener, UITextFieldDelegate>
@property (nonatomic, strong) UILabel *banner;
@property (nonatomic, strong) UILabel *loginUserInfo;
@property (nonatomic, strong) UIView *sendArea;

@property (nonatomic, strong) UITextField *sendId;
@property (nonatomic, strong) UITextField *sendMsg;
@end

@implementation ViewController{
    BOOL isLogin;
    
    NSString *userId;
    NSString *sig;
    NSTimer *loginTimer;
    
    TIMHepler *tim;
}

-(instancetype)init{
    if (self = [super init]) {
        isLogin = NO;
        
        tim = [TIMHepler new];
        [tim loginServiceWithAccount:@"zz001"];
        
        NSDictionary *userInfo = @{@"account":@"zz001"};
        loginTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(searchLoginUser:) userInfo:userInfo repeats:YES];
        [loginTimer fire];
        
    }
    return self;
}

- (void)searchLoginUser:(NSTimer *)timer{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    userId = [userDefaults objectForKey:@"IM_USERID"];
    sig = [userDefaults objectForKey:@"IM_SIG"];
    
    if (userId) {
        [loginTimer invalidate];
        isLogin = YES;
        
        self.banner.hidden = NO;
        
        self.loginUserInfo.text = [NSString stringWithFormat:@"你的IM登录ID: %@", userId];
        self.loginUserInfo.hidden = NO;
        
        self.sendArea.hidden = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
}


-(void)sendMsg:(UIButton *)btn{
    [tim sendMessage:self.sendMsg.text withUserId:self.sendId.text];
}

-(UILabel *)banner{
    if (!_banner) {
        _banner = ({
            UILabel *banner = [UILabel new];
            banner.text = @"已登录平台及腾讯云通信!";
            banner.hidden = YES;
            banner.textColor = [UIColor blackColor];
            banner.font = [UIFont systemFontOfSize:17];
            
            [self.view addSubview:banner];
            [banner mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view).offset(100);
                make.centerX.equalTo(self.view);
            }];
            banner;
        });
    }
    return _banner;
}

-(UILabel *)loginUserInfo{
    if (!_loginUserInfo) {
        _loginUserInfo = ({
            UILabel *loginUserInfo = [UILabel new];
            loginUserInfo.hidden = YES;
            loginUserInfo.textColor = [UIColor blackColor];
            loginUserInfo.font = [UIFont systemFontOfSize:17];
            
            [self.view addSubview:loginUserInfo];
            [loginUserInfo mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.banner.mas_bottom).offset(60);
                make.centerX.equalTo(self.view);
            }];
            
            loginUserInfo;
        });
    }
    return _loginUserInfo;
}

-(UIView *)sendArea{
    if (!_sendArea) {
        _sendArea = ({
            UIView *area = [UIView new];
            area.hidden = YES;
            area.backgroundColor = [UIColor lightGrayColor];
            
            UITextField *field = [UITextField new];
            field.placeholder = @"输入对方IM ID";
            [area addSubview:field];
            [field mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(area);
                make.centerX.equalTo(area);
            }];
            self.sendId = field;
            
            UITextField *fieldMsg = [UITextField new];
            fieldMsg.placeholder = @"输入信息";
            [area addSubview:fieldMsg];
            [fieldMsg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(field.mas_bottom).offset(10);
                make.centerX.equalTo(area);
            }];
            self.sendMsg = fieldMsg;
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:@"发送" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(sendMsg:) forControlEvents:UIControlEventTouchDown];
            
            [area addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(fieldMsg.mas_bottom).offset(10);
                make.centerX.equalTo(area);
                make.bottom.equalTo(area);
            }];
            
            
            [self.view addSubview:area];
            [area mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.loginUserInfo.mas_bottom).offset(30);
                make.centerX.equalTo(self.view);
                make.width.equalTo(self.view);
            }];
            
            area;
        });
    }
    return _sendArea;
}
@end
